﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig
{
    public partial class VideoSetting : Form
    {
        public int intIndex;

        public VideoSetting(int index)
        {
            InitializeComponent();
            intIndex = index;
            this.cbbContent1.Properties.Items.AddRange(Common.VideoContentSelection);
            this.cbbContent2.Properties.Items.AddRange(Common.VideoContentSelection);
            this.cbbContent3.Properties.Items.AddRange(Common.VideoContentSelection);
            this.cbbContent4.Properties.Items.AddRange(Common.VideoContentSelection);
            this.cbbContent1.SelectedIndex = 0;
            this.cbbContent2.SelectedIndex = 0;
            this.cbbContent3.SelectedIndex = 0;
            this.cbbContent4.SelectedIndex = 0;
            SetValue();
        }

        /// <summary>
        /// 设置摄像头配置
        /// </summary>
        public void SetValue()
        {
            try
            {
                if (intIndex == 0)
                {
                    ipAddressInput1.Value = ParameterConfig.rtuConfigInfo.VideoIP1[0].ToString() + "." + ParameterConfig.rtuConfigInfo.VideoIP1[1].ToString() + "." + ParameterConfig.rtuConfigInfo.VideoIP1[2].ToString() + "." + ParameterConfig.rtuConfigInfo.VideoIP1[3].ToString();
                    nudPort.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoPort1.Reverse<byte>().ToArray(), 0);
                    txtUserName.Text = new string(ParameterConfig.rtuConfigInfo.VideoUserName1).Replace("\0", "");
                    txtPassWord.Text = new string(ParameterConfig.rtuConfigInfo.VideoPassWord1).Replace("\0", "");
                    txtContent1.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName11).Replace("\0", "");
                    txtContent2.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName12).Replace("\0", "");
                    txtContent3.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName13).Replace("\0", "");
                    txtContent4.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName14).Replace("\0", "");
                    cbbContent1.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection1[0];
                    cbbContent2.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection1[1];
                    cbbContent3.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection1[2];
                    cbbContent4.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection1[3];
                    nud1.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate11_X.Reverse<byte>().ToArray(), 0);
                    nud11.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate11_Y.Reverse<byte>().ToArray(), 0);
                    nud2.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate12_X.Reverse<byte>().ToArray(), 0);
                    nud21.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate12_Y.Reverse<byte>().ToArray(), 0);
                    nud3.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate13_X.Reverse<byte>().ToArray(), 0);
                    nud31.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate13_Y.Reverse<byte>().ToArray(), 0);
                    nud4.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate14_X.Reverse<byte>().ToArray(), 0);
                    nud41.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate14_Y.Reverse<byte>().ToArray(), 0);
                }

                if (intIndex == 1)
                {
                    ipAddressInput1.Value = ParameterConfig.rtuConfigInfo.VideoIP2[0].ToString() + "." + ParameterConfig.rtuConfigInfo.VideoIP2[1].ToString() + "." + ParameterConfig.rtuConfigInfo.VideoIP2[2].ToString() + "." + ParameterConfig.rtuConfigInfo.VideoIP2[3].ToString();
                    nudPort.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoPort2.Reverse<byte>().ToArray(), 0);
                    txtUserName.Text = new string(ParameterConfig.rtuConfigInfo.VideoUserName2).Replace("\0", "");
                    txtPassWord.Text = new string(ParameterConfig.rtuConfigInfo.VideoPassWord2).Replace("\0", "");
                    txtContent1.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName21).Replace("\0", "");
                    txtContent2.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName22).Replace("\0", "");
                    txtContent3.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName23).Replace("\0", "");
                    txtContent4.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName24).Replace("\0", "");
                    cbbContent1.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection2[0];
                    cbbContent2.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection2[1];
                    cbbContent3.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection2[2];
                    cbbContent4.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection2[3];
                    nud1.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate21_X.Reverse<byte>().ToArray(), 0);
                    nud11.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate21_Y.Reverse<byte>().ToArray(), 0);
                    nud2.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate22_X.Reverse<byte>().ToArray(), 0);
                    nud21.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate22_Y.Reverse<byte>().ToArray(), 0);
                    nud3.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate23_X.Reverse<byte>().ToArray(), 0);
                    nud31.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate23_Y.Reverse<byte>().ToArray(), 0);
                    nud4.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate24_X.Reverse<byte>().ToArray(), 0);
                    nud41.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate24_Y.Reverse<byte>().ToArray(), 0);
                }
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置参数失败！");
            }
        }

        /// <summary>
        /// 获取摄像头配置
        /// </summary>
        public void GetValue()
        {
            string[] strValue = ipAddressInput1.Value.Trim().Split(new char[] { '.' });
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.VideoIP1[0] = Convert.ToByte(strValue[0]);
                ParameterConfig.rtuConfigInfo.VideoIP1[1] = Convert.ToByte(strValue[1]);
                ParameterConfig.rtuConfigInfo.VideoIP1[2] = Convert.ToByte(strValue[2]);
                ParameterConfig.rtuConfigInfo.VideoIP1[3] = Convert.ToByte(strValue[3]);
                ParameterConfig.rtuConfigInfo.VideoUserName1 = txtUserName.Text.PadRight(20, '\0').ToCharArray();
                ParameterConfig.rtuConfigInfo.VideoPassWord1 = txtPassWord.Text.PadRight(20, '\0').ToCharArray();
                ParameterConfig.rtuConfigInfo.VideoPort1 = BitConverter.GetBytes(Convert.ToUInt16(nudPort.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentSelection1[0] = Convert.ToByte(cbbContent1.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection1[1] = Convert.ToByte(cbbContent2.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection1[2] = Convert.ToByte(cbbContent3.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection1[3] = Convert.ToByte(cbbContent4.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoOverlayName11 = Common.GetByteForString(txtContent1.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName12 = Common.GetByteForString(txtContent2.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName13 = Common.GetByteForString(txtContent3.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName14 = Common.GetByteForString(txtContent4.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate11_X = BitConverter.GetBytes(Convert.ToUInt16(nud1.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate11_Y = BitConverter.GetBytes(Convert.ToUInt16(nud11.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate12_X = BitConverter.GetBytes(Convert.ToUInt16(nud2.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate12_Y = BitConverter.GetBytes(Convert.ToUInt16(nud21.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate13_X = BitConverter.GetBytes(Convert.ToUInt16(nud3.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate13_Y = BitConverter.GetBytes(Convert.ToUInt16(nud31.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate14_X = BitConverter.GetBytes(Convert.ToUInt16(nud4.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate14_Y = BitConverter.GetBytes(Convert.ToUInt16(nud41.Value)).Reverse<byte>().ToArray();
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.VideoIP2[0] = Convert.ToByte(strValue[0]);
                ParameterConfig.rtuConfigInfo.VideoIP2[1] = Convert.ToByte(strValue[1]);
                ParameterConfig.rtuConfigInfo.VideoIP2[2] = Convert.ToByte(strValue[2]);
                ParameterConfig.rtuConfigInfo.VideoIP2[3] = Convert.ToByte(strValue[3]);
                ParameterConfig.rtuConfigInfo.VideoUserName2 = txtUserName.Text.PadRight(20, '\0').ToCharArray();
                ParameterConfig.rtuConfigInfo.VideoPassWord2 = txtPassWord.Text.PadRight(20, '\0').ToCharArray();
                ParameterConfig.rtuConfigInfo.VideoPort2 = BitConverter.GetBytes(Convert.ToUInt16(nudPort.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentSelection2[0] = Convert.ToByte(cbbContent1.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection2[1] = Convert.ToByte(cbbContent2.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection2[2] = Convert.ToByte(cbbContent3.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection2[3] = Convert.ToByte(cbbContent4.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoOverlayName21 = Common.GetByteForString(txtContent1.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName22 = Common.GetByteForString(txtContent2.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName23 = Common.GetByteForString(txtContent3.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName24 = Common.GetByteForString(txtContent4.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate21_X = BitConverter.GetBytes(Convert.ToUInt16(nud1.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate21_Y = BitConverter.GetBytes(Convert.ToUInt16(nud11.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate22_X = BitConverter.GetBytes(Convert.ToUInt16(nud2.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate22_Y = BitConverter.GetBytes(Convert.ToUInt16(nud21.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate23_X = BitConverter.GetBytes(Convert.ToUInt16(nud3.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate23_Y = BitConverter.GetBytes(Convert.ToUInt16(nud31.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate24_X = BitConverter.GetBytes(Convert.ToUInt16(nud4.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate24_Y = BitConverter.GetBytes(Convert.ToUInt16(nud41.Value)).Reverse<byte>().ToArray();
            }
        }

        /// <summary>
        /// 关闭前保存修改的配置信息
        /// </summary>
        private void WaterLevelSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ipAddressInput1.Value == null)
            {
                Common.ShowErrorMessage("请输入摄像头IP地址！");
                e.Cancel = true;
                return;
            }

            if (txtUserName.Text != string.Empty)
            {
                for (int i = 0; i < txtUserName.Text.Length; i++)
                {
                    if (txtUserName.Text[i] < ' ' || txtUserName.Text[i] > '~')
                    {
                        Common.ShowErrorMessage("用户名必须为ASCII字符！");
                        e.Cancel = true;
                        return;
                    }
                }
            }

            if (txtPassWord.Text != string.Empty)
            {
                for (int i = 0; i < txtPassWord.Text.Length; i++)
                {
                    if (txtPassWord.Text[i] < ' ' || txtPassWord.Text[i] > '~')
                    {
                        Common.ShowErrorMessage("密码必须为ASCII字符！");
                        e.Cancel = true;
                        return;
                    }
                }
            }

            GetValue();
            this.Dispose();
        }
    }
}
